<?php
namespace ZfcUser\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;
use Zend\Paginator\Paginator;
class TuserTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function resetpassword($email,$password)
	{
		$this->tableGateway->update(array("password"=>$password),array('email_id' => $email));
		return $this->tableGateway->lastInsertValue;
	}
	public function addUser($user,$token='',$type='',$filename='')
    {		
		if($type=='account'){
			$emailId=$user['Mentor_Email'];
			$name=$user['Mentor_Name'];
			$mobileNo=$user['phonecode'].'-'.$user['Mentor_Number'];
			$usertype=$user['Relationship'];
			$address=null;
			$country=null;
			$state=null;
			$city=null;
			$gender=null;
			$board_id=null;
			$class_id=null;
			$school_name=null;
			$user_photo=null;
			$aboutme=null;
			$postalcode=null;
			$status=0;
		}else if($type=='update'){
			$emailId=$user['useremail'];
			$name=$user['username'];
			if(isset($user['phonecode']))
			{
			$mobileNo=$user['phonecode'].'-'.$user['phonenumber'];
			}
			else
			{
				$mobileNo=$user['phonenumber'];
			}
			$usertype=$user['usertype'];
			$country=$user['ucountries'];
			$state=$user['states'];
			$city=$user['cities'];
			if(isset($_SESSION['user']['user_type_name']) && $_SESSION['user']['user_type_name']=='Student'){
				$gender=$user['sex'];
			}else{
				$gender=null;
			}			
			$board_id=$user['board'];
			$class_id=$user['classnames'];
			$school_name=$user['userschool'];
			$user_photo=$filename;
			$address=null;
			$postalcode=$user['postalcode'];
			if(isset($_SESSION['user']['user_type_name']) && $_SESSION['user']['user_type_name']=='Parent'){
				$aboutme=$user['aboutme'];
			}else{
				$aboutme=null;
			}
			$status=1;
		}else{
			$emailId=$user['emailId'];
			$name=$user['name'];
			if(isset($user['phonecode']))
			{
			$mobileNo=$user['phonecode'].'-'.$user['phone'];
			}
			else
			{
				$mobileNo=$user['phone'];
			}
			$usertype=$user['usertype'];
			if(isset($user['country']) && $user['country']!='')
			{
				$country=$user['country'];
			}else
			{
				$country=null;
			}
			if(isset($user['state']) && $user['state']!='')
			{
				$state=$user['state'];
			}else
			{
				$state=null;
			}
			if(isset($user['city']) && $user['city']!='')
			{
				$city=$user['city'];
			}else
			{
			$city=null;
			}
			if(isset($user['address']) && $user['address']!='')
			{
				$address=$user['address'];
			}else
			{
			$address=null;
			}
			$gender=null;
			if(isset($user['board']) && $user['board']!='')
			{
				$board_id=$user['board'];
			}else
			{
			$board_id=null;
			}
			if(isset($user['class']) && $user['class']!='')
			{
				$class_id=$user['class'];
			}else
			{
			$class_id=null;
			}
			$school_name=null;
			$user_photo=null;
			$aboutme=null;
			$status=0;
			$postalcode=null;
		}					
		$password=md5($token);
		$data = array(
			'country'  				=> 	$country,
			'city'  				=> 	$city,
			'state'  				=> 	$state,
			'create_time'			=>	date('Y-m-d H:i:s'), 
			'dob'					=> 	null, 	
			'email_id'				=> 	$emailId,  	
			'email_validation'		=> 	$emailId,   
			'father_name'			=> 	null,
			'first_name'			=> 	$name,  
            'gender'				=>	$gender,
            'ip'					=>	$_SERVER['REMOTE_ADDR'],
            'last_name'				=>	null,
            'login_id'				=>	null,
            'mobile'				=>	$mobileNo,			
            'user_status'			=>	$status,
            'surname'				=>  null,
            'validation_code'		=>  null,
            'validation_time'		=>  null,
			'user_type_id'          =>	$usertype,
			'board_id'              =>	$board_id,
			'class_id'              =>	$class_id,
			'user_photo'            =>	$user_photo,
			'aboutme'               =>	$aboutme,
			'postalcode'            =>	$postalcode,
		);		
		if(isset($user['hiduserid'])){if($user['hiduserid']!=""){
			if(isset($user['address']) && $user['address']!=''){
				$data['address']=$address;
			}
			if(isset($user['userschool']) && $user['userschool']!=''){
				$data['school_name']=$school_name;
			}			
			$row=$this->tableGateway->update($data, array('user_id' => $user['hiduserid']));
		}}else{			
			$data['address']=$address;
			$data['school_name']=$school_name;
			$data['password']=$password;				
			$this->tableGateway->insert($data);		
			$select = $this->tableGateway->getSql()->select()
			->where('email_id="'.$emailId.'"')
			->where('password="'.$password.'"');			
			$resultSet = $this->tableGateway->selectWith($select);			
			$row = $resultSet->current();			
		}		
		return $row;
    }
	public function checkEmail($email,$module)
    {	
	 $select = $this->tableGateway->getSql()->select()			
			->where('email_id="'.$email.'"');					 
		$resultSet = $this->tableGateway->selectWith($select);
		if($module=='account'){
			$row = $resultSet;
		}else{
			$row = $resultSet->count();
		}
		return $row;		
	}
	public function activeUser($id)
    {	
	 $data = array(
				'user_id'      =>	$id,
				'user_status'  =>	1,
			);
		$updateuserid=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$updateuserid;			
	}
	public function getpassword($pwd,$userid){ 
		$pwd=md5($pwd);
		$select = $this->tableGateway->getSql()->select();
		$select->where('password="'.$pwd.'"');
		$select->where('user_id="'.$userid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->count();			
		return $row;
	}
	public function changepwd($pwd,$userid){
		$password=md5($pwd);
		$data = array(
				'user_id'       =>$userid,
				'password'      =>$password,
				);
		$changepassword=$this->tableGateway->update($data, array('user_id' => $data['user_id']));
		return 	$changepassword;			
	}
	public function checkLogin($user)
    {		
		$password=md5($user['userPassword']);
		$select = $this->tableGateway->getSql()->select()
				->where('email_id="'.$user['userEmail'].'"')
				->where('password="'.$password.'"');
		$resultSet = $this->tableGateway->selectWith($select);				
        $row = $resultSet->current();		
        return $row;
	}
	public function checkregLogin($emailid,$pwd)
    {		
		$password=md5($pwd);
		$select = $this->tableGateway->getSql()->select()
				->where('email_id="'.$emailid.'"')
				->where('password="'.$password.'"');
		$resultSet = $this->tableGateway->selectWith($select);				
        $row = $resultSet->current();		
        return $row;
	}
	public function getMentorNames()
    {		
		$select = $this->tableGateway->getSql()->select()
				->where('user_type_id!="1"');
		$resultSet = $this->tableGateway->selectWith($select);				
        return $resultSet;
	}
	public function checkMentor($mentorName,$mId)
    {		
		$select = $this->tableGateway->getSql()->select()
				->join('t_user_type', 't_user.user_type_id=t_user_type.user_type_id',array('*'),'left')
				->where('first_name="'.$mentorName.'"');
		$resultSet = $this->tableGateway->selectWith($select);				
        return $resultSet;
	}
	public function getuserid($emailid){
	
		$select = $this->tableGateway->getSql()->select();		
		$select->where('email_id="'.$emailid.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();		 
        return $row;
	}
	public function getuserdetails($userid){
		$select = $this->tableGateway->getSql()->select();
		$select	->join('t_user_type', 't_user.user_type_id=t_user_type.user_type_id',array('*'),'left');
		$select	->join(array("rackboard"=>'resource_rack'), 't_user.board_id=rackboard.rack_id',array('*'),'left');
		$select	->join(array("rackboardname"=>'rack_name'), 'rackboardname.rack_name_id=rackboard.rack_name_id',array('board_name' => new Expression('rackboardname.name')),'left');
		$select	->join(array("rackclass"=>'resource_rack'), 't_user.class_id=rackclass.rack_id',array('*'),'left');
		$select	->join(array("rackclassname"=>'rack_name'), 'rackclassname.rack_name_id=rackclass.rack_name_id',array('class_name' => new Expression('rackclassname.name')),'left');
		$select	->join('t_country', 't_user.country=t_country.country_id',array('*'),'left');
		$select	->join('t_state', 't_user.state=t_state.state_id',array('*'),'left');
		$select	->join('t_cities', 't_user.city=t_cities.city_id',array('*'),'left');
		if(isset($_SESSION['user']['user_type_name']) && $_SESSION['user']['user_type_name']=='Parent'){
			$select	->join('t_student_and_mentor', 't_user.user_id=t_student_and_mentor.mentor_id',array('*'),'left');
			$select	->join(array("usertable"=>'t_user'), 'usertable.user_id=t_student_and_mentor.student_id',array('nameChild' => new Expression('usertable.first_name')),'left');
			$select->where('t_user.user_id="'.$userid.'"');
		}else{
			$select->where('t_user.user_id="'.$userid.'"');
		}
		$row = $this->tableGateway->selectWith($select);
        return $row;
	}
	public function getuserdetailsChild($userid){
		$select = $this->tableGateway->getSql()->select();
		$select	->join('t_student_and_mentor', 't_user.user_id=t_student_and_mentor.student_id',array('nameChild' =>new Expression('t_user.first_name')),'left');
		$select->where('t_student_and_mentor.mentor_id="'.$userid.'"');
		$row = $this->tableGateway->selectWith($select);		
        return $row;
	}
	public function updateBoardClassUser($user)
    {
		$data = array(
			'board_id'              =>	$user['boardId'],
			'class_id'              =>	$user['classId'],
		);			
		$row=$this->tableGateway->update($data, array('user_id' => $_SESSION['user']['userId']));
		return $row;
    }
}